package com.thoughtworks.Chapter1;

import com.thoughtworks.Common.Apple;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * Created by richie on 10/7/16.
 */
public class FilteringApples {

    public void Show() {
        List<Apple> inventory = Arrays.asList(
                new Apple(80, "gree"),
                new Apple(155, "gree"),
                new Apple(120, "red"));


        List<Apple> greenApples=filterApples(inventory,apple->apple.getWeight()>150);
        List<Apple> greenApples2=filterApples(inventory,FilteringApples::isGreenApple);

        List<Apple> heavyApples=filterApples(inventory,apple -> apple.getWeight()>150);
        List<Apple> heavyApples2=filterApples(inventory,FilteringApples::isHeavyApple);


    }

    public static boolean isGreenApple(Apple apple) {
        return "green".equals(apple.getColor());
    }

    public static boolean isHeavyApple(Apple apple) {
        return apple.getWeight()>150;
    }

    public List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> predicate) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if(predicate.test(apple)){
                result.add(apple);
            }
        }
        return result;

    }
}
